/* Title:		
 *
 * Project:    
 *
 * @ Link: 		http://...
 *
 * @ Email:		ivan_ling@hotmail.com
 *
 * @ Copyright: Copyright (c) 2008 mezimedia
 *
 * @ Author 	Ivan.ling
 *
 * @ Version 	1.0
 
 * @ last change time 2008-01-20 	
 */

package com.pub.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


public class DateUtils
{
	private static final String ZONE_ETC_GREEWICH = "Etc/Greenwich";
	
	private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	private static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static final String DEFAULT_DATE_TIME_ZONE_FORMAT = "yyyy-MM-dd HH:mm:ss z";
	
	public static String getNowFormatTime(String strTimeFormat, int field, int amount)
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(strTimeFormat);
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.add(field, amount);
		
		return sdfFormat.format(cdCalendar.getTime());
	}
	
	public static String getGreenwichNow()
	{
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_TIME_ZONE_FORMAT);   
		sdf.setTimeZone(TimeZone.getTimeZone(ZONE_ETC_GREEWICH));
		return sdf.format(new Date());
	}
	
	public static String getGreenwichNow(int hourOffset)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_TIME_ZONE_FORMAT);
		sdf.setTimeZone(TimeZone.getTimeZone(ZONE_ETC_GREEWICH));
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.add(Calendar.HOUR_OF_DAY, hourOffset);
		//Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("Etc/Greenwich"));   
		//calendar.setTimeZone(TimeZone.getTimeZone("Etc/Greenwich")); 
		
		return sdf.format(cdCalendar.getTime());    
	}
	

	public static String getNowFormatTime(String strTimeFormat)
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(strTimeFormat);
		Calendar cdCalendar = Calendar.getInstance();

		return sdfFormat.format(cdCalendar.getTime());
	}
	
	public static String getNowDate()
	{
		return DateUtils.getNowFormatTime(DEFAULT_DATE_FORMAT);
	}
	
	public static String getNowTime()
	{
		return DateUtils.getNowFormatTime(DEFAULT_DATE_TIME_FORMAT);
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compare and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static boolean isCompareByNow(Date dtInput, int iField, int iAmount)
	{
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTime(dtInput);
		cdCalendar.add(iField, iAmount);
		
		return cdCalendar.getTime().after(new Date());
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compare and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static boolean isBeforeByNow(Date dtInput)
	{
		return new Date().before(dtInput);
	}
	
	public static long loDateSubByNow(Date dtPrimary, String strDate) throws ParseException
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
		Date dtInput = sdfFormat.parse(strDate);
		long lDateSub = dtPrimary.getTime() - dtInput.getTime();
		
		return lDateSub / 1000;
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compare and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static boolean compare(String sourceTime, String targetTime, int iField, int iAmount) throws ParseException
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
		Date dtSource 	= sdfFormat.parse(sourceTime);
		Date dtTarget 	= sdfFormat.parse(targetTime);
		
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTime(dtSource);
		cdCalendar.add(iField, iAmount);
		
		return cdCalendar.getTime().after(dtTarget);
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compare and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static boolean compare(Timestamp sourceTime, Timestamp targetTime, int iAmount) throws ParseException
	{
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTimeInMillis(sourceTime.getTime());
		cdCalendar.add(Calendar.SECOND, iAmount);
		
		return cdCalendar.getTime().after(targetTime);
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compare and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static boolean compare(Timestamp sourceTime, Timestamp targetTime, int iField, int iAmount) throws ParseException
	{
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTimeInMillis(sourceTime.getTime());
		cdCalendar.add(iField, iAmount);
		
		return cdCalendar.getTime().after(targetTime);
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compare and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static boolean compare(String strDateFormat, String sourceTime, String targetTime, int iField, int iAmount) throws ParseException
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(strDateFormat);
		Date dtSource 	= sdfFormat.parse(sourceTime);
		Date dtTarget 		= sdfFormat.parse(targetTime);
		
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTime(dtSource);
		cdCalendar.add(iField, iAmount);
		
		return cdCalendar.getTime().after(dtTarget);
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compar and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static String getAddSubCal(String strDateFormat, String strTime, int iField, int iAmount) throws ParseException
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(strDateFormat);
		Date dtInput 	= sdfFormat.parse(strTime);
		
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTime(dtInput);
		cdCalendar.add(iField, iAmount);
		
		return sdfFormat.format(cdCalendar.getTime());
	}
	
	/*
	 * strDateFormat	date format
	 * strDate			date of compar and now date
	 * iField			date calculate field
	 * iAmount			date calculate value of field.
	 * return 			input time > now time : true , input time < now time fail
	 */
	public static String getAddSubCal(String strTime, int iField, int iAmount) throws ParseException
	{
		SimpleDateFormat sdfFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
		Date dtInput 	= sdfFormat.parse(strTime);
		
		Calendar cdCalendar = Calendar.getInstance();
		cdCalendar.setTime(dtInput);
		cdCalendar.add(iField, iAmount);
		
		return sdfFormat.format(cdCalendar.getTime());
	}
	
	/*
	 * strDate 	control date
	 * iAmount	control round, unit second.
	 */
	public static boolean compare(String sourceTime, String targetTime, int iAmount) throws ParseException
	{
		return DateUtils.compare(sourceTime, targetTime, Calendar.SECOND, iAmount);
	}
	
	public static boolean isFirstDay()
	{
		return DateUtils.getDay().equals("01") ? true: false;
	}
	
	public static String getDay()
	{
		Calendar calCalendar = Calendar.getInstance();
		SimpleDateFormat sdfFormat = new SimpleDateFormat("dd");
		return sdfFormat.format(calCalendar.getTime());
	}
	
	public static String getCustomTimeFromNow(int iField, int iAmount, String strReturnFormat)
	{
		Calendar calCalendar = Calendar.getInstance();
		calCalendar.add(iField, iAmount);
		SimpleDateFormat sdfFormat = new SimpleDateFormat(strReturnFormat);
		return sdfFormat.format(calCalendar.getTime());
	}
	
	public static String getYesterday(String date)
	{
		String yesterday = "";
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
		Calendar objectday = Calendar.getInstance();
		
		String[] dates = date.split("-");
		
		if(dates.length == 3){
			int year 	= Integer.parseInt(dates[0]);
			int month 	= Integer.parseInt(dates[1]) - 1;
			String[] daytime = dates[2].split(" ");
			int day = Integer.parseInt(daytime[0]);
			objectday.set(year, month, day);
			
			objectday.add(Calendar.DAY_OF_YEAR, -1);
		}
		
		yesterday = format.format(objectday.getTime());
		
		return yesterday;
	}
	
	public static void main( String [] args )
	{
		//System.out.println("====" + getCustomTimeFromNow(Calendar.HOUR, 0, "HH"));
		//System.out.println("====" + getGreenwichNow(-18));
		
		try {
			//System.out.println("====" + compare("2009-09-29 11:41:01", "2009-09-29 11:40:00", -60));
			
			System.out.println("====" + compare(Timestamp.valueOf("2009-09-29 11:41:00"), Timestamp.valueOf("2009-09-29 11:40:00"), Calendar.SECOND, -60));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}